<?php
class WAPAlipay{
	public $notify_url   = '';//异步处理地址
	public $return_url   = '';//同步处理地址
	public $show_url     = '';//商品展示地址
	// public $seller_email = 'mk@qingtajiao.com';//收款支付宝账号
	// public $partner      = '2088712582583250';//合作身份者id
	// public $key          = 'fo5v0t1uezebyzwysdv81o2s1cpuqbiw';//安全检验码

	public $seller_email = 'pay@qingtajiao.com';//收款支付宝账号
	public $partner      = '2088021075351093';//合作身份者id
	public $key          = 't38pd2dfva3nmlbtp8k7zuddsj63b0l6';//安全检验码
	public $sign_type    = 'MD5';//签名方式
	public $cacert_path  = '';//签名方式
	public $transport    = 'http';//访问模式
	public $is_debug     = false;//true为调试模式，价格会设置成0.01，false为非调试模式，价格按正常来
	public $logs_path    = '';

	public function __construct() {
        //导入支付宝alipay_submit包
		import("alipay_submit",'Public/Class/Pay/Alipay/lib/WAP');
        //导入支付宝alipay_notify包
		import("alipay_notify",'Public/Class/Pay/Alipay/lib/WAP');
		//日志保存目录
		$this->logs_path = dirname(__FILE__).'/Logs/';
		if(file_exists($this->logs_path)==false){
			mkdir($this->logs_path,true);
		}
    }
	/**
	 * 平台商支付宝配置
	 */
	public function config($company_id=''){
		//支付宝账号
		$_config['seller_email']        = $this->seller_email;
		//合作身份者id，以2088开头的16位纯数字
		$_config['partner']             = $this->partner;
		//安全检验码，以数字和字母组成的32位字符
		$_config['key']                 = $this->key;
		//签名方式 不需修改
		$_config['sign_type']           = strtoupper($this->sign_type);
		//字符编码格式 目前支持 gbk 或 utf-8
		$_config['input_charset']       = strtolower('utf-8');
		//ca证书路径地址，用于curl中ssl校验
		//请保证cacert.pem文件在当前文件夹目录中
		$cacert_path                    = dirname(__FILE__).'/alipay_cacert.pem';
		$_config['cacert']              = $this->cacert_path?$this->cacert_path:$cacert_path;
		//访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
		$_config['transport']           = $this->transport;
		//商户的私钥（后缀是.pen）文件相对路径,如果签名方式设置为“0001”时，请设置该参数
		$_config['private_key_path']    = '';
		//支付宝公钥（后缀是.pen）文件相对路径,如果签名方式设置为“0001”时，请设置该参数
		$_config['ali_public_key_path'] = '';
		
		return $_config;
	}
	public function submit($order_sn,$order_title,$payed){
		//调试模式价格设置0.01元
		if($this->is_debug==true){
			$payed = 0.01;
		}
		$alipay_config = $this->config();
		/**************************请求参数**************************/
		//返回格式
		$format = "xml";
		//返回格式
		$v = "2.0";
		//请求号，必填，须保证每次请求都是唯一
		$req_id = date('Ymdhis');

		//服务器异步通知页面路径
		$notify_url    = $this->notify_url ? $this->notify_url : U("Alipay/notify");
		//页面跳转同步通知页面路径
		$call_back_url = $this->return_url ? $this->return_url : U("Alipay/call_back");
		//操作中断返回地址
		$merchant_url  = $this->merchant_url ? $this->merchant_url : U("Alipay/merchant");
		//卖家支付宝帐户
		$seller_email  = $alipay_config['seller_email'];
		//请求业务参数详细
		$req_data = '<direct_trade_create_req><notify_url>' . $notify_url . '</notify_url><call_back_url>' . $call_back_url . '</call_back_url><seller_account_name>' . $seller_email . '</seller_account_name><out_trade_no>' . $order_sn . '</out_trade_no><subject>' . $order_title . '</subject><total_fee>' . $payed . '</total_fee><merchant_url>' . $merchant_url . '</merchant_url></direct_trade_create_req>';
		/************************************************************/

		//构造要请求的参数数组，无需改动
		$para_token = array(
				"service"        => "alipay.wap.trade.create.direct",
				"partner"        => trim($alipay_config['partner']),
				"sec_id"         => trim($alipay_config['sign_type']),
				"format"         => $format,
				"v"              => $v,
				"req_id"         => $req_id,
				"req_data"       => $req_data,
				"_input_charset" => trim(strtolower($alipay_config['input_charset']))
		);
		// var_dump($para_token);exit;

		//建立请求
		$alipaySubmit = new AlipaySubmit($alipay_config);
		$html_text = $alipaySubmit->buildRequestHttp($para_token);
		//URLDECODE返回的信息
		$html_text = urldecode($html_text);
		//解析远程模拟提交后返回的信息
		$para_html_text = $alipaySubmit->parseResponse($html_text);
		//获取request_token
		$request_token = $para_html_text['request_token'];

		/**************************根据授权码token调用交易接口alipay.wap.auth.authAndExecute**************************/

		//业务详细
		$req_data = '<auth_and_execute_req><request_token>' . $request_token . '</request_token></auth_and_execute_req>';
		//必填

		//构造要请求的参数数组，无需改动
		$parameter = array(
				"service"        => "alipay.wap.auth.authAndExecute",
				"partner"        => trim($alipay_config['partner']),
				"sec_id"         => trim($alipay_config['sign_type']),
				"format"         => $format,
				"v"              => $v,
				"req_id"         => $req_id,
				"req_data"       => $req_data,
				"_input_charset" => trim(strtolower($alipay_config['input_charset']))
		);

		//建立请求
		$alipaySubmit = new AlipaySubmit($alipay_config);
		$alipaySubmit->jump_pay($parameter);
	}
}
?>